package com.chenyi.user.service.impl;

import java.util.List;

import com.chenyi.common.constant.Constants;
import com.chenyi.common.core.domain.entity.SysAdmin;
import com.chenyi.common.core.domain.model.LoginUser;
import com.chenyi.common.core.redis.RedisCache;
import com.chenyi.common.enums.UserStatus;
import com.chenyi.common.exception.BadRequestException;
import com.chenyi.common.utils.ApiUtils;
import com.chenyi.common.utils.DateUtils;
import com.chenyi.common.utils.MessageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.chenyi.user.mapper.SysUserMapper;
import com.chenyi.common.core.domain.entity.SysUser;
import com.chenyi.user.service.ISysUserService;

import javax.annotation.PostConstruct;

/**
 * 用户信息Service业务层处理
 * 
 * @author wangkh
 * @date 2020-10-10
 */
@Service
public class SysUserServiceImpl implements ISysUserService 
{
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 项目启动时，初始化参数到缓存
     */
    @PostConstruct
    public void init()
    {
        String userNo = sysUserMapper.selectFinallyUserNo();
        redisCache.setCacheObject("sys_no:user_no",userNo);
    }


    /**
     * 查询用户信息
     * 
     * @param userId 用户信息ID
     * @return 用户信息
     */
    @Override
    public SysUser selectUserById(Long userId)
    {
        return sysUserMapper.selectUserById(userId);
    }

    /**
     * 查询用户信息列表
     * 
     * @param sysUser 用户信息
     * @return 用户信息
     */
    @Override
    public List<SysUser> selectUserList(SysUser sysUser)
    {
        return sysUserMapper.selectUserList(sysUser);
    }

    /**
     * 新增用户信息
     * 
     * @param sysUser 用户信息
     * @return 结果
     */
    @Override
    public int insertUser(SysUser sysUser)
    {
        sysUser.setCreateTime(DateUtils.getNowDate());
        sysUser.setUserNo(getUserNo());
        return sysUserMapper.insertUser(sysUser);
    }

    /**
     * 修改用户信息
     * 
     * @param sysUser 用户信息
     * @return 结果
     */
    @Override
    public int updateUser(SysUser sysUser)
    {
        sysUser.setUpdateTime(DateUtils.getNowDate());
        return sysUserMapper.updateUser(sysUser);
    }

    /**
     * 批量删除用户信息
     * 
     * @param userIds 需要删除的用户信息ID
     * @return 结果
     */
    @Override
    public int deleteUserByIds(Long[] userIds)
    {
        return sysUserMapper.deleteUserByIds(userIds);
    }

    /**
     * 删除用户信息信息
     * 
     * @param userId 用户信息ID
     * @return 结果
     */
    @Override
    public int deleteUserById(Long userId)
    {
        return sysUserMapper.deleteUserById(userId);
    }

    @Override
    public SysUser selectUserByPhone(String phone, String userType) {
        return sysUserMapper.selectUserByPhone(phone,userType);
    }

    @Override
    public SysUser selectUserByToken(String token) {
        return sysUserMapper.selectUserByToken(token);
    }

    @Override
    public int updateUserStatus(SysUser user) {
        return sysUserMapper.updateUser(user);
    }

    @Override
    public int resetPwd(SysUser user) {
        return sysUserMapper.updateUser(user);
    }


    /* 功能描述: <br>
     * 〈获取用户编号〉
     *
     * @param
     * @return: java.lang.String
     * @Author: wangkh
     * @Date: 2020/11/2 0002 15:29
     */
    private String getUserNo(){
        String sysNo = redisCache.getCacheObject("sys_no:user_no");
        String prefix = sysNo.substring(0,2);
        Integer userNo = Integer.parseInt(sysNo.substring(2)) + 1;
        return prefix + userNo;
    }
}
